package com.nl.model.model.page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * 分页默认实现类.
 *
 * @param <T> 类型
 * @author breggor
 */
@Data
@NoArgsConstructor
@Accessors(chain = true)
@ApiModel(description = "分页数据")
public final class PaginationImpl<T> implements Pagination<T>, Serializable {
    private static final long serialVersionUID = 1L;

    private static final long DEFAULT_PAGE_SIZE = 10L;

    /**
     * 每页行数.
     */
    @ApiModelProperty(value = "每页行数")
    private long size = DEFAULT_PAGE_SIZE;

    /**
     * 总行数.
     */
    @ApiModelProperty(value = "总记录数")
    private long total;

    /**
     * 数据列表.
     */
    @ApiModelProperty(value = "数据集合")
    private List<T> records = Collections.emptyList();

    public PaginationImpl(final long size, final long total, final List<T> records) {
        this.size = (size <= 0) ? DEFAULT_PAGE_SIZE : size;
        this.total = total;
        this.records = records;
    }

    @Override
    public long getPages() {
        if (getSize() == 0L) {
            return 0L;
        }

        return (getTotal() % getSize() != 0L)
            ? (getTotal() / getSize() + 1L)
            : getTotal() / getSize();
    }
}
